<?php
namespace app\web\controller;

use think\Db;

/**
 * 月卡次卡项目共享控制器
 * @package app\index\controller
 */
class ViperProjectShare extends Auth
{
    public $noNeedLogin = '';


    /**
     * 获取会员卡类型共享列表
     */
    public function lists(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            if(!isset($getData['old_store_id']) || empty($getData['old_store_id'])){
                $this->result('缺少参数','',204);
            }
            $data = Db::name('qt_viper_project_share')
                ->field('vs.id,p.name as old_project_name,new_project_id,old_project_id')
                ->alias('vs')
                ->leftJoin('qt_store s','s.id = vs.old_store_id')
                ->leftJoin('qt_project p','p.id = vs.old_project_id')
                ->where(['vs.old_store_id'=>$getData['old_store_id']])
                ->paginate($getData['limit'])->each(function ($item){
                    $item['new_project_name'] = Db::name('qt_project')->where('id',$item['new_project_id'])->value('name');
                    return $item;
                });
            $this->result('请求成功',$data,200);
        }
        $this->result('请求错误','',204);
    }




    /**
     * 保存会员卡类型共享列表
     */
    public function save(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if(empty($data['old_store_id']) || empty($data['project'])){
                $this->result('缺少参数','',204);
            }
            $project = json_decode($data['project'],true);
            foreach ($project as $key=>$value){
                $value['new_store_id'] = $this->userInfo['store_id'];
                $value['old_store_id'] = $data['old_store_id'];
                Db::name('qt_viper_share')->where('id',$value['id'])->update($value);
            }
            $this->result('保存成功',$data,200);
        }
        $this->result('请求错误','',204);
    }


}
